home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / MediaTracker.java < prev    next >
Text File  |  1998-09-22  |  31KB  |  924 lines

  1. /*
  2.  * @(#)MediaTracker.java    1.27 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.awt;
  16.  
  17. import java.awt.Component;
  18. import java.awt.Image;
  19. import java.awt.Graphics;
  20. import java.awt.image.ImageObserver;
  21.  
  22. /**
  23.  * The <code>MediaTracker</code> class is a utility class to track 
  24.  * the status of a number of media objects. Media objects could 
  25.  * include audio clips as well as images, though currently only 
  26.  * images are supported. 
  27.  * <p>
  28.  * To use a media tracker, create an instance of  
  29.  * <code>MediaTracker</code> and call its <code>addImage</code>  
  30.  * method for each image to be tracked. In addition, each image can 
  31.  * be assigned a unique identifier. This identifier controls the 
  32.  * priority order in which the images are fetched. It can also be used 
  33.  * to identify unique subsets of the images that can be waited on 
  34.  * independently. Images with a lower ID are loaded in preference to 
  35.  * those with a higher ID number. 
  36.  * <p>
  37.  * Here is an example: 
  38.  * <p>
  39.  * <hr><blockquote><pre>
  40.  * import java.applet.Applet;
  41.  * import java.awt.Color;
  42.  * import java.awt.Image;
  43.  * import java.awt.Graphics;
  44.  * import java.awt.MediaTracker;
  45.  *
  46.  * public class ImageBlaster extends Applet implements Runnable {
  47.  *    MediaTracker tracker;
  48.  *    Image bg;
  49.  *    Image anim[] = new Image[5];
  50.  *    int index;
  51.  *    Thread animator;
  52.  *
  53.  *    // Get the images for the background (id == 0) 
  54.  *    // and the animation frames (id == 1) 
  55.  *      // and add them to the MediaTracker
  56.  *    public void init() {
  57.  *        tracker = new MediaTracker(this);
  58.  *        bg = getImage(getDocumentBase(), 
  59.  *                  "images/background.gif");
  60.  *        tracker.addImage(bg, 0);
  61.  *        for (int i = 0; i < 5; i++) {
  62.  *        anim[i] = getImage(getDocumentBase(), 
  63.  *                      "images/anim"+i+".gif");
  64.  *        tracker.addImage(anim[i], 1);
  65.  *        }
  66.  *    }
  67.  *
  68.  *    // Start the animation thread.
  69.  *    public void start() {
  70.  *        animator = new Thread(this);
  71.  *        animator.start();
  72.  *    }
  73.  *
  74.  *    // Stop the animation thread.
  75.  *    public void stop() {
  76.  *        animator.stop();
  77.  *        animator = null;
  78.  *    }
  79.  *
  80.  *    // Run the animation thread.
  81.  *    // First wait for the background image to fully load 
  82.  *      // and paint.  Then wait for all of the animation 
  83.  *    // frames to finish loading. Finally, loop and 
  84.  *    // increment the animation frame index.
  85.  *    public void run() {
  86.  *        try {
  87.  *        tracker.waitForID(0);
  88.  *        tracker.waitForID(1);
  89.  *        } catch (InterruptedException e) {
  90.  *        return;
  91.  *        }
  92.  *        Thread me = Thread.currentThread();
  93.  *        while (animator == me) {
  94.  *        try {
  95.  *            Thread.sleep(100);
  96.  *        } catch (InterruptedException e) {
  97.  *            break;
  98.  *        }
  99.  *        synchronized (this) {
  100.  *            index++;
  101.  *            if (index >= anim.length) {
  102.  *            index = 0;
  103.  *            }
  104.  *        }
  105.  *        repaint();
  106.  *        }
  107.  *    }
  108.  *
  109.  *    // The background image fills the frame so we 
  110.  *    // don't need to clear the applet on repaints. 
  111.  *      // Just call the paint method.
  112.  *    public void update(Graphics g) {
  113.  *        paint(g);
  114.  *    }
  115.  *
  116.  *    // Paint a large red rectangle if there are any errors 
  117.  *    // loading the images.  Otherwise always paint the 
  118.  *    // background so that it appears incrementally as it 
  119.  *      // is loading.  Finally, only paint the current animation 
  120.  *    // frame if all of the frames (id == 1) are done loading,
  121.  *    // so that we don't get partial animations.
  122.  *    public void paint(Graphics g) {
  123.  *        if ((tracker.statusAll(false) & MediaTracker.ERRORED) != 0) {
  124.  *        g.setColor(Color.red);
  125.  *        g.fillRect(0, 0, size().width, size().height);
  126.  *        return;
  127.  *        }
  128.  *        g.drawImage(bg, 0, 0, this);
  129.  *        if (tracker.statusID(1, false) == MediaTracker.COMPLETE) {
  130.  *        g.drawImage(anim[index], 10, 10, this);
  131.  *        }
  132.  *    }
  133.  * }
  134.  * </pre></blockquote><hr>
  135.  * <p>
  136.  * @version     1.27, 07/01/98
  137.  * @author     Jim Graham
  138.  * @since       JDK1.0
  139.  */
  140. public class MediaTracker implements java.io.Serializable {
  141.     Component target;
  142.     MediaEntry head;
  143.  
  144.     /*
  145.      * JDK 1.1 serialVersionUID 
  146.      */
  147.     private static final long serialVersionUID = -483174189758638095L;
  148.  
  149.     /**
  150.      * Creates a media tracker to track images for a given component.
  151.      * @param     comp the component on which the images 
  152.      *                     will eventually be drawn.
  153.      * @since     JDK1.0
  154.      */
  155.     public MediaTracker(Component comp) {
  156.     target = comp;
  157.     }
  158.  
  159.     /**
  160.      * Adds an image to the list of images being tracked by this media 
  161.      * tracker. The image will eventually be rendered at its default 
  162.      * (unscaled) size. 
  163.      * @param     image   the image to be tracked.
  164.      * @param     id      an identifier used to track this image.
  165.      * @since     JDK1.0
  166.      */
  167.     public void addImage(Image image, int id) {
  168.     addImage(image, id, -1, -1);
  169.     }
  170.  
  171.     /**
  172.      * Adds a scaled image to the list of images being tracked  
  173.      * by this media tracker. The image will eventually be 
  174.      * rendered at the indicated width and height.
  175.      * @param     image   the image to be tracked.
  176.      * @param     id   an identifier that can be used to track this image.
  177.      * @param     w    the width at which the image is rendered.
  178.      * @param     h    the height at which the image is rendered.
  179.      * @since     JDK1.0
  180.      */
  181.     public synchronized void addImage(Image image, int id, int w, int h) {
  182.     head = MediaEntry.insert(head,
  183.                  new ImageMediaEntry(this, image, id, w, h));
  184.     }
  185.  
  186.     /**
  187.      * Flag indicating some media is currently being loaded.
  188.      * @see         java.awt.MediaTracker#statusAll
  189.      * @see         java.awt.MediaTracker#statusID
  190.      * @since       JDK1.0
  191.      */
  192.     public static final int LOADING = 1;
  193.  
  194.     /**
  195.      * Flag indicating that the downloading of some media was aborted.
  196.      * @see         java.awt.MediaTracker#statusAll
  197.      * @see         java.awt.MediaTracker#statusID
  198.      * @since       JDK1.0
  199.      */
  200.     public static final int ABORTED = 2;
  201.  
  202.     /**
  203.      * Flag indicating that the downloading of some media encountered 
  204.      * an error.
  205.      * @see         java.awt.MediaTracker#statusAll
  206.      * @see         java.awt.MediaTracker#statusID
  207.      * @since       JDK1.0
  208.      */
  209.     public static final int ERRORED = 4;
  210.  
  211.     /**
  212.      * Flag indicating that the downloading of media was completed 
  213.      * successfully.
  214.      * @see         java.awt.MediaTracker#statusAll
  215.      * @see         java.awt.MediaTracker#statusID
  216.      * @since       JDK1.0
  217.      */
  218.     public static final int COMPLETE = 8;
  219.  
  220.     static final int DONE = (ABORTED | ERRORED | COMPLETE);
  221.  
  222.     /**
  223.      * Checks to see if all images being tracked by this media tracker 
  224.      * have finished loading. 
  225.      * <p>
  226.      * This method does not start loading the images if they are not 
  227.      * already loading. 
  228.      * <p>
  229.      * If there is an error while loading or scaling an image, then that 
  230.      * image is considered to have finished loading. Use the 
  231.      * <code>isErrorAny</code> or <code>isErrorID</code> methods to 
  232.      * check for errors. 
  233.      * @return      <code>true</code> if all images have finished loading, 
  234.      *                       have been aborted, or have encountered 
  235.      *                       an error; <code>false</code> otherwise.
  236.      * @see         java.awt.MediaTracker#checkAll(boolean)
  237.      * @see         java.awt.MediaTracker#checkID
  238.      * @see         java.awt.MediaTracker#isErrorAny
  239.      * @see         java.awt.MediaTracker#isErrorID
  240.      * @since       JDK1.0
  241.      */
  242.     public boolean checkAll() {
  243.     return checkAll(false, true);
  244.     }
  245.  
  246.     /**
  247.      * Checks to see if all images being tracked by this media tracker 
  248.      * have finished loading. 
  249.      * <p>
  250.      * If the value of the <code>load</code> flag is <code>true</code>, 
  251.      * then this method starts loading any images that are not yet 
  252.      * being loaded. 
  253.      * <p>
  254.      * If there is an error while loading or scaling an image, that 
  255.      * image is considered to have finished loading. Use the 
  256.      * <code>isErrorAny</code> and <code>isErrorID</code> methods to 
  257.      * check for errors. 
  258.      * @param       load   if <code>true</code>, start loading any 
  259.      *                       images that are not yet being loaded.
  260.      * @return      <code>true</code> if all images have finished loading, 
  261.      *                       have been aborted, or have encountered 
  262.      *                       an error; <code>false</code> otherwise.
  263.      * @see         java.awt.MediaTracker#checkID
  264.      * @see         java.awt.MediaTracker#checkAll()
  265.      * @see         java.awt.MediaTracker#isErrorAny()
  266.      * @see         java.awt.MediaTracker#isErrorID(int)
  267.      * @since       JDK1.0
  268.      */
  269.     public boolean checkAll(boolean load) {
  270.     return checkAll(load, true);
  271.     }
  272.  
  273.     private synchronized boolean checkAll(boolean load, boolean verify) {
  274.     MediaEntry cur = head;
  275.     boolean done = true;
  276.     while (cur != null) {
  277.         if ((cur.getStatus(load, verify) & DONE) == 0) {
  278.         done = false;
  279.         }
  280.         cur = cur.next;
  281.     }
  282.     return done;
  283.     }
  284.  
  285.     /**
  286.      * Checks the error status of all of the images.
  287.      * @return   <code>true</code> if any of the images tracked
  288.      *                  by this media tracker had an error during 
  289.      *                  loading; <code>false</code> otherwise.
  290.      * @see      java.awt.MediaTracker#isErrorID
  291.      * @see      java.awt.MediaTracker#getErrorsAny
  292.      * @since    JDK1.0
  293.      */
  294.     public synchronized boolean isErrorAny() {
  295.     MediaEntry cur = head;
  296.     while (cur != null) {
  297.         if ((cur.getStatus(false, true) & ERRORED) != 0) {
  298.         return true;
  299.         }
  300.         cur = cur.next;
  301.     }
  302.     return false;
  303.     }
  304.  
  305.     /**
  306.      * Returns a list of all media that have encountered an error.
  307.      * @return       an array of media objects tracked by this 
  308.      *                        media tracker that have encountered 
  309.      *                        an error, or <code>null</code> if 
  310.      *                        there are none with errors.
  311.      * @see          java.awt.MediaTracker#isErrorAny
  312.      * @see          java.awt.MediaTracker#getErrorsID
  313.      * @since        JDK1.0
  314.      */
  315.     public synchronized Object[] getErrorsAny() {
  316.     MediaEntry cur = head;
  317.     int numerrors = 0;
  318.     while (cur != null) {
  319.         if ((cur.getStatus(false, true) & ERRORED) != 0) {
  320.         numerrors++;
  321.         }
  322.         cur = cur.next;
  323.     }
  324.     if (numerrors == 0) {
  325.         return null;
  326.     }
  327.     Object errors[] = new Object[numerrors];
  328.     cur = head;
  329.     numerrors = 0;
  330.     while (cur != null) {
  331.         if ((cur.getStatus(false, false) & ERRORED) != 0) {
  332.         errors[numerrors++] = cur.getMedia();
  333.         }
  334.         cur = cur.next;
  335.     }
  336.     return errors;
  337.     }
  338.  
  339.     /**
  340.      * Starts loading all images tracked by this media tracker. This 
  341.      * method waits until all the images being tracked have finished 
  342.      * loading. 
  343.      * <p>
  344.      * If there is an error while loading or scaling an image, then that 
  345.      * image is considered to have finished loading. Use the 
  346.      * <code>isErrorAny</code> or <code>isErrorID</code> methods to 
  347.      * check for errors. 
  348.      * @see         java.awt.MediaTracker#waitForID(int)
  349.      * @see         java.awt.MediaTracker#waitForAll(long)
  350.      * @see         java.awt.MediaTracker#isErrorAny
  351.      * @see         java.awt.MediaTracker#isErrorID
  352.      * @exception   InterruptedException  if another thread has 
  353.      *                                     interrupted this thread.
  354.      * @since       JDK1.0
  355.      */
  356.     public void waitForAll() throws InterruptedException {
  357.     waitForAll(0);
  358.     }
  359.  
  360.     /**
  361.      * Starts loading all images tracked by this media tracker. This 
  362.      * method waits until all the images being tracked have finished 
  363.      * loading, or until the length of time specified in milliseconds  
  364.      * by the <code>ms</code> argument has passed. 
  365.      * <p>
  366.      * If there is an error while loading or scaling an image, then  
  367.      * that image is considered to have finished loading. Use the 
  368.      * <code>isErrorAny</code> or <code>isErrorID</code> methods to 
  369.      * check for errors. 
  370.      * @param       ms       the number of milliseconds to wait 
  371.      *                       for the loading to complete.
  372.      * @return      <code>true</code> if all images were successfully 
  373.      *                       loaded; <code>false</code> otherwise.
  374.      * @see         java.awt.MediaTracker#waitForID(int)
  375.      * @see         java.awt.MediaTracker#waitForAll(long)
  376.      * @see         java.awt.MediaTracker#isErrorAny
  377.      * @see         java.awt.MediaTracker#isErrorID
  378.      * @exception   InterruptedException  if another thread has 
  379.      *                                     interrupted this thread.
  380.      * @since       JDK1.0
  381.      */
  382.     public synchronized boolean waitForAll(long ms)
  383.     throws InterruptedException
  384.     {
  385.     long end = System.currentTimeMillis() + ms;
  386.     boolean first = true;
  387.     while (true) {
  388.         int status = statusAll(first, first);
  389.         if ((status & LOADING) == 0) {
  390.         return (status == COMPLETE);
  391.         }
  392.         first = false;
  393.         long timeout;
  394.         if (ms == 0) {
  395.         timeout = 0;
  396.         } else {
  397.         timeout = end - System.currentTimeMillis();
  398.         if (timeout <= 0) {
  399.             return false;
  400.         }
  401.         }
  402.         wait(timeout);
  403.     }
  404.     }
  405.  
  406.     /**
  407.      * Calculates and returns the bitwise inclusive <b>OR</b> of the 
  408.      * status of all media that are tracked by this media tracker. 
  409.      * <p>
  410.      * Possible flags defined by the 
  411.      * <code>MediaTracker</code> class are <code>LOADING</code>, 
  412.      * <code>ABORTED</code>, <code>ERRORED</code>, and 
  413.      * <code>COMPLETE</code>. An image that hasn't started 
  414.      * loading has zero as its status. 
  415.      * <p>
  416.      * If the value of <code>load</code> is <code>true</code>, then
  417.      * this method starts loading any images that are not yet being loaded. 
  418.      * @param        load   if <code>true</code>, start loading 
  419.      *                            any images that are not yet being loaded.
  420.      * @return       the bitwise inclusive <b>OR</b> of the status of 
  421.      *                            all of the media being tracked.
  422.      * @see          java.awt.MediaTracker#statusID(int, boolean)
  423.      * @see          java.awt.MediaTracker#LOADING
  424.      * @see          java.awt.MediaTracker#ABORTED
  425.      * @see          java.awt.MediaTracker#ERRORED
  426.      * @see          java.awt.MediaTracker#COMPLETE
  427.      * @since        JDK1.0
  428.      */
  429.     public int statusAll(boolean load) {
  430.     return statusAll(load, true);
  431.     }
  432.  
  433.     private synchronized int statusAll(boolean load, boolean verify) {
  434.     MediaEntry cur = head;
  435.     int status = 0;
  436.     while (cur != null) {
  437.         status = status | cur.getStatus(load, verify);
  438.         cur = cur.next;
  439.     }
  440.     return status;
  441.     }
  442.  
  443.     /**
  444.      * Checks to see if all images tracked by this media tracker that 
  445.      * are tagged with the specified identifier have finished loading. 
  446.      * <p>
  447.      * This method does not start loading the images if they are not 
  448.      * already loading. 
  449.      * <p>
  450.      * If there is an error while loading or scaling an image, then that 
  451.      * image is considered to have finished loading. Use the 
  452.      * <code>isErrorAny</code> or <code>isErrorID</code> methods to 
  453.      * check for errors. 
  454.      * @param       id   the identifier of the images to check.
  455.      * @return      <code>true</code> if all images have finished loading, 
  456.      *                       have been aborted, or have encountered 
  457.      *                       an error; <code>false</code> otherwise.
  458.      * @see         java.awt.MediaTracker#checkID(int, boolean)
  459.      * @see         java.awt.MediaTracker#checkAll()
  460.      * @see         java.awt.MediaTracker#isErrorAny()
  461.      * @see         java.awt.MediaTracker#isErrorID(int)
  462.      * @since       JDK1.0
  463.      */
  464.     public boolean checkID(int id) {
  465.     return checkID(id, false, true);
  466.     }
  467.  
  468.     /**
  469.      * Checks to see if all images tracked by this media tracker that 
  470.      * are tagged with the specified identifier have finished loading. 
  471.      * <p>
  472.      * If the value of the <code>load</code> flag is <code>true</code>, 
  473.      * then this method starts loading any images that are not yet 
  474.      * being loaded. 
  475.      * <p>
  476.      * If there is an error while loading or scaling an image, then that 
  477.      * image is considered to have finished loading. Use the 
  478.      * <code>isErrorAny</code> or <code>isErrorID</code> methods to 
  479.      * check for errors. 
  480.      * @param       id       the identifier of the images to check.
  481.      * @param       load     if <code>true</code>, start loading any 
  482.      *                       images that are not yet being loaded.
  483.      * @return      <code>true</code> if all images have finished loading, 
  484.      *                       have been aborted, or have encountered 
  485.      *                       an error; <code>false</code> otherwise.
  486.      * @see         java.awt.MediaTracker#checkID(int, boolean)
  487.      * @see         java.awt.MediaTracker#checkAll()
  488.      * @see         java.awt.MediaTracker#isErrorAny()
  489.      * @see         java.awt.MediaTracker#isErrorID(int)
  490.      * @since       JDK1.0
  491.      */
  492.     public boolean checkID(int id, boolean load) {
  493.     return checkID(id, load, true);
  494.     }
  495.  
  496.     private synchronized boolean checkID(int id, boolean load, boolean verify)
  497.     {
  498.     MediaEntry cur = head;
  499.     boolean done = true;
  500.     while (cur != null) {
  501.         if (cur.getID() == id
  502.         && (cur.getStatus(load, verify) & DONE) == 0)
  503.         {
  504.         done = false;
  505.         }
  506.         cur = cur.next;
  507.     }
  508.     return done;
  509.     }
  510.  
  511.     /**
  512.      * Checks the error status of all of the images tracked by this 
  513.      * media tracker with the specified identifier. 
  514.      * @param        id   the identifier of the images to check.
  515.      * @return       <code>true</code> if any of the images with the 
  516.      *                          specified identifier had an error during 
  517.      *                          loading; <code>false</code> otherwise.
  518.      * @see          java.awt.MediaTracker#isErrorAny
  519.      * @see          java.awt.MediaTracker#getErrorsID
  520.      * @since        JDK1.0
  521.      */
  522.     public synchronized boolean isErrorID(int id) {
  523.     MediaEntry cur = head;
  524.     while (cur != null) {
  525.         if (cur.getID() == id
  526.         && (cur.getStatus(false, true) & ERRORED) != 0)
  527.         {
  528.         return true;
  529.         }
  530.         cur = cur.next;
  531.     }
  532.     return false;
  533.     }
  534.  
  535.     /**
  536.      * Returns a list of media with the specified ID that 
  537.      * have encountered an error.
  538.      * @param       id   the identifier of the images to check.
  539.      * @return      an array of media objects tracked by this media 
  540.      *                       tracker with the specified identifier 
  541.      *                       that have encountered an error, or 
  542.      *                       <code>null</code> if there are none with errors.
  543.      * @see         java.awt.MediaTracker#isErrorID
  544.      * @see         java.awt.MediaTracker#isErrorAny
  545.      * @see         java.awt.MediaTracker#getErrorsAny
  546.      * @since       JDK1.0
  547.      */
  548.     public synchronized Object[] getErrorsID(int id) {
  549.     MediaEntry cur = head;
  550.     int numerrors = 0;
  551.     while (cur != null) {
  552.         if (cur.getID() == id
  553.         && (cur.getStatus(false, true) & ERRORED) != 0)
  554.         {
  555.         numerrors++;
  556.         }
  557.         cur = cur.next;
  558.     }
  559.     if (numerrors == 0) {
  560.         return null;
  561.     }
  562.     Object errors[] = new Object[numerrors];
  563.     cur = head;
  564.     numerrors = 0;
  565.     while (cur != null) {
  566.         if (cur.getID() == id
  567.         && (cur.getStatus(false, false) & ERRORED) != 0)
  568.         {
  569.         errors[numerrors++] = cur.getMedia();
  570.         }
  571.         cur = cur.next;
  572.     }
  573.     return errors;
  574.     }
  575.  
  576.     /**
  577.      * Starts loading all images tracked by this media tracker with the 
  578.      * specified identifier. This method waits until all the images with 
  579.      * the specified identifier have finished loading. 
  580.      * <p>
  581.      * If there is an error while loading or scaling an image, then that 
  582.      * image is considered to have finished loading. Use the 
  583.      * <code>isErrorAny</code> and <code>isErrorID</code> methods to 
  584.      * check for errors. 
  585.      * @param         id   the identifier of the images to check.
  586.      * @see           java.awt.MediaTracker#waitForAll
  587.      * @see           java.awt.MediaTracker#isErrorAny()
  588.      * @see           java.awt.MediaTracker#isErrorID(int)
  589.      * @exception     InterruptedException  if another thread has 
  590.      *                          interrupted this thread.
  591.      * @since         JDK1.0
  592.      */
  593.     public void waitForID(int id) throws InterruptedException {
  594.     waitForID(id, 0);
  595.     }
  596.  
  597.     /**
  598.      * Starts loading all images tracked by this media tracker with the 
  599.      * specified identifier. This method waits until all the images with 
  600.      * the specified identifier have finished loading, or until the 
  601.      * length of time specified in milliseconds by the <code>ms</code> 
  602.      * argument has passed. 
  603.      * <p>
  604.      * If there is an error while loading or scaling an image, then that 
  605.      * image is considered to have finished loading. Use the 
  606.      * <code>statusID</code>, <code>isErrorID</code>, and
  607.      * <code>isErrorAny</code> methods to check for errors. 
  608.      * @param         id   the identifier of the images to check.
  609.      * @param         ms   the length of time, in milliseconds, to wait 
  610.      *                           for the loading to complete.
  611.      * @see           java.awt.MediaTracker#waitForAll
  612.      * @see           java.awt.MediaTracker#waitForID(int)
  613.      * @see           java.awt.MediaTracker#statusID
  614.      * @see           java.awt.MediaTracker#isErrorAny()
  615.      * @see           java.awt.MediaTracker#isErrorID(int)
  616.      * @exception     InterruptedException  if another thread has 
  617.      *                          interrupted this thread.
  618.      * @since         JDK1.0 
  619.      */
  620.     public synchronized boolean waitForID(int id, long ms)
  621.     throws InterruptedException
  622.     {
  623.     long end = System.currentTimeMillis() + ms;
  624.     boolean first = true;
  625.     while (true) {
  626.         int status = statusID(id, first, first);
  627.         if ((status & LOADING) == 0) {
  628.         return (status == COMPLETE);
  629.         }
  630.         first = false;
  631.         long timeout;
  632.         if (ms == 0) {
  633.         timeout = 0;
  634.         } else {
  635.         timeout = end - System.currentTimeMillis();
  636.         if (timeout <= 0) {
  637.             return false;
  638.         }
  639.         }
  640.         wait(timeout);
  641.     }
  642.     }
  643.  
  644.     /**
  645.      * Calculates and returns the bitwise inclusive <b>OR</b> of the 
  646.      * status of all media with the specified identifier that are 
  647.      * tracked by this media tracker. 
  648.      * <p>
  649.      * Possible flags defined by the 
  650.      * <code>MediaTracker</code> class are <code>LOADING</code>, 
  651.      * <code>ABORTED</code>, <code>ERRORED</code>, and 
  652.      * <code>COMPLETE</code>. An image that hasn't started 
  653.      * loading has zero as its status. 
  654.      * <p>
  655.      * If the value of <code>load</code> is <code>true</code>, then
  656.      * this method starts loading any images that are not yet being loaded. 
  657.      * @param        id   the identifier of the images to check.
  658.      * @param        load   if <code>true</code>, start loading 
  659.      *                            any images that are not yet being loaded.
  660.      * @return       the bitwise inclusive <b>OR</b> of the status of 
  661.      *                            all of the media with the specified
  662.      *                            identifier that are being tracked.
  663.      * @see          java.awt.MediaTracker#statusAll(boolean)
  664.      * @see          java.awt.MediaTracker#LOADING
  665.      * @see          java.awt.MediaTracker#ABORTED
  666.      * @see          java.awt.MediaTracker#ERRORED
  667.      * @see          java.awt.MediaTracker#COMPLETE
  668.      * @since        JDK1.0
  669.      */
  670.     public int statusID(int id, boolean load) {
  671.     return statusID(id, load, true);
  672.     }
  673.  
  674.     private synchronized int statusID(int id, boolean load, boolean verify) {
  675.     MediaEntry cur = head;
  676.     int status = 0;
  677.     while (cur != null) {
  678.         if (cur.getID() == id) {
  679.         status = status | cur.getStatus(load, verify);
  680.         }
  681.         cur = cur.next;
  682.     }
  683.     return status;
  684.     }
  685.  
  686.     /**
  687.      * Remove the specified image from this media tracker.
  688.      * All instances of the specified image are removed, 
  689.      * regardless of scale or ID.
  690.      * @param   image     the image to be removed
  691.      * @see     java.awt.MediaTracker#removeImage(java.awt.Image, int)
  692.      * @see     java.awt.MediaTracker#removeImage(java.awt.Image, int, int, int)
  693.      * @since   JDK1.1
  694.      */
  695.     public synchronized void removeImage(Image image) {
  696.     MediaEntry cur = head;
  697.     MediaEntry prev = null;
  698.     while (cur != null) {
  699.         MediaEntry next = cur.next;
  700.         if (cur.getMedia() == image) {
  701.         if (prev == null) {
  702.             head = next;
  703.         } else {
  704.             prev.next = next;
  705.         }
  706.         cur.cancel();
  707.         } else {
  708.         prev = cur;
  709.         }
  710.         cur = next;
  711.     }
  712.     notifyAll();    // Notify in case remaining images are "done".
  713.     }
  714.  
  715.     /**
  716.      * Remove the specified image from the specified tracking 
  717.      * ID of this media tracker.
  718.      * All instances of <code>Image</code> being tracked 
  719.      * under the specified ID are removed regardless of scale.
  720.      * @param      image the image to be removed.
  721.      * @param      id the tracking ID frrom which to remove the image.
  722.      * @see        java.awt.MediaTracker#removeImage(java.awt.Image)
  723.      * @see        java.awt.MediaTracker#removeImage(java.awt.Image, int, int, int)
  724.      * @since      JDK1.1
  725.      */
  726.     public synchronized void removeImage(Image image, int id) {
  727.     MediaEntry cur = head;
  728.     MediaEntry prev = null;
  729.     while (cur != null) {
  730.         MediaEntry next = cur.next;
  731.         if (cur.getID() == id && cur.getMedia() == image) {
  732.         if (prev == null) {
  733.             head = next;
  734.         } else {
  735.             prev.next = next;
  736.         }
  737.         cur.cancel();
  738.         } else {
  739.         prev = cur;
  740.         }
  741.         cur = next;
  742.     }
  743.     notifyAll();    // Notify in case remaining images are "done".
  744.     }
  745.  
  746.     /**
  747.      * Remove the specified image with the specified 
  748.      * width, height, and ID from this media tracker.
  749.      * Only the specified instance (with any duplicates) is removed.
  750.      * @param   image the image to be removed
  751.      * @param   id the tracking ID from which to remove the image.
  752.      * @param   width the width to remove (-1 for unscaled).
  753.      * @param   height the height to remove (-1 for unscaled).
  754.      * @see     java.awt.MediaTracker#removeImage(java.awt.Image)
  755.      * @see     java.awt.MediaTracker#removeImage(java.awt.Image, int)
  756.      * @since   JDK1.1
  757.      */
  758.     public synchronized void removeImage(Image image, int id,
  759.                      int width, int height) {
  760.     MediaEntry cur = head;
  761.     MediaEntry prev = null;
  762.     while (cur != null) {
  763.         MediaEntry next = cur.next;
  764.         if (cur.getID() == id && cur instanceof ImageMediaEntry
  765.         && ((ImageMediaEntry) cur).matches(image, width, height))
  766.         {
  767.         if (prev == null) {
  768.             head = next;
  769.         } else {
  770.             prev.next = next;
  771.         }
  772.         cur.cancel();
  773.         } else {
  774.         prev = cur;
  775.         }
  776.         cur = next;
  777.     }
  778.     notifyAll();    // Notify in case remaining images are "done".
  779.     }
  780.  
  781.     synchronized void setDone() {
  782.     notifyAll();
  783.     }
  784. }
  785.  
  786. abstract class MediaEntry {
  787.     MediaTracker tracker;
  788.     int ID;
  789.     MediaEntry next;
  790.  
  791.     int status;
  792.     boolean cancelled;
  793.  
  794.     /*
  795.      * JDK 1.1 serialVersionUID 
  796.      */
  797.     private static final long serialVersionUID = -2924957284304726459L;
  798.  
  799.     MediaEntry(MediaTracker mt, int id) {
  800.     tracker = mt;
  801.     ID = id;
  802.     }
  803.  
  804.     abstract Object getMedia();
  805.  
  806.     static MediaEntry insert(MediaEntry head, MediaEntry me) {
  807.     MediaEntry cur = head;
  808.     MediaEntry prev = null;
  809.     while (cur != null) {
  810.         if (cur.ID > me.ID) {
  811.         break;
  812.         }
  813.         prev = cur;
  814.         cur = cur.next;
  815.     }
  816.     me.next = cur;
  817.     if (prev == null) {
  818.         head = me;
  819.     } else {
  820.         prev.next = me;
  821.     }
  822.     return head;
  823.     }
  824.  
  825.     int getID() {
  826.     return ID;
  827.     }
  828.  
  829.     abstract void startLoad();
  830.  
  831.     void cancel() {
  832.     cancelled = true;
  833.     }
  834.  
  835.     static final int LOADING = MediaTracker.LOADING;
  836.     static final int ABORTED = MediaTracker.ABORTED;
  837.     static final int ERRORED = MediaTracker.ERRORED;
  838.     static final int COMPLETE = MediaTracker.COMPLETE;
  839.  
  840.     static final int LOADSTARTED = (LOADING | ERRORED | COMPLETE);
  841.     static final int DONE = (ABORTED | ERRORED | COMPLETE);
  842.  
  843.     synchronized int getStatus(boolean doLoad, boolean doVerify) {
  844.     if (doLoad && ((status & LOADSTARTED) == 0)) {
  845.         status = (status & ~ABORTED) | LOADING;
  846.         startLoad();
  847.     }
  848.     return status;
  849.     }
  850.  
  851.     void setStatus(int flag) {
  852.     synchronized (this) {
  853.         status = flag;
  854.     }
  855.     tracker.setDone();
  856.     }
  857. }
  858.  
  859. class ImageMediaEntry extends MediaEntry implements ImageObserver, 
  860. java.io.Serializable {
  861.     Image image;
  862.     int width;
  863.     int height;
  864.  
  865.     ImageMediaEntry(MediaTracker mt, Image img, int c, int w, int h) {
  866.     super(mt, c);
  867.     image = img;
  868.     width = w;
  869.     height = h;
  870.     }
  871.  
  872.     boolean matches(Image img, int w, int h) {
  873.     return (image == img && width == w && height == h);
  874.     }
  875.  
  876.     Object getMedia() {
  877.     return image;
  878.     }
  879.  
  880.     int getStatus(boolean doLoad, boolean doVerify) {
  881.     if (doVerify) {
  882.         int flags = tracker.target.checkImage(image, width, height, this);
  883.         int s = parseflags(flags);
  884.         if (s == 0) {
  885.         if ((status & (ERRORED | COMPLETE)) != 0) {
  886.             setStatus(ABORTED);
  887.         }
  888.         } else if (s != status) {
  889.         setStatus(s);
  890.         }
  891.     }
  892.     return super.getStatus(doLoad, doVerify);
  893.     }
  894.  
  895.     void startLoad() {
  896.     if (tracker.target.prepareImage(image, width, height, this)) {
  897.         setStatus(COMPLETE);
  898.     }
  899.     }
  900.  
  901.     int parseflags(int infoflags) {
  902.     if ((infoflags & ERROR) != 0) {
  903.         return ERRORED;
  904.     } else if ((infoflags & ABORT) != 0) {
  905.         return ABORTED;
  906.     } else if ((infoflags & (ALLBITS | FRAMEBITS)) != 0) {
  907.         return COMPLETE;
  908.     }
  909.     return 0;
  910.     }
  911.  
  912.     public boolean imageUpdate(Image img, int infoflags,
  913.                    int x, int y, int w, int h) {
  914.     if (cancelled) {
  915.         return false;
  916.     }
  917.     int s = parseflags(infoflags);
  918.     if (s != 0 && s != status) {
  919.         setStatus(s);
  920.     }
  921.     return ((status & LOADING) != 0);
  922.     }
  923. }
  924.